﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using ToDoList.Logic;
namespace ToDoListTest
{
    public class Program
    {        
        public static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            Initialize init = new Initialize();
            do
            {
                Console.WriteLine("1.用户登录\t2.用户注册\t0.退出");
                switch (Convert.ToInt32(Console.ReadLine()))
                {
                    case 1: UserSystem(); break;                                     
                    case 2: PrintRegister(); break;
                    case 0: Environment.Exit(0); break;
                    default: Console.WriteLine("输入有误！！！"); break;
                }
            } while (true);
        }

        /// <summary>
        /// 打印登陆所需
        /// </summary>
        /// <returns></returns>
        public static int PrintLogic()
        {
            UserLogic user = new UserLogic();
            Console.Write("输入ID：");
            int UserId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入密码：");
            string Password = Console.ReadLine();
            if (user.Login(UserId, Password))
                return UserId;
            else
                return 0;
        }
        /// <summary>
        /// 打印注册所需
        /// </summary>
        public static void PrintRegister()
        {
            UserLogic user = new UserLogic();
            Console.Write("输入昵称：");
            string NikeName = Console.ReadLine();
            Console.Write("输入密码：");
            string Password1 = Console.ReadLine();
            Console.Write("请再次确定密码：");
            string Password2 = Console.ReadLine();
            if (Password1.Equals(Password2))
            {
                Console.Write("请牢记你的ID：");
                Console.WriteLine(user.Register(Password2, NikeName));
                Console.WriteLine("注册成功！！！");
            }
            else
                Console.WriteLine("请确定两次输入的密码是否一致！");
        }
        /// <summary>
        /// 打印未完成事项
        /// </summary>
        public static void PrintUnfinishedItem()
        {
            UserLogic user = new UserLogic();
            var UnfinishedItem = user.UnfinishedItem(Initialize.ItemList);
            if (UnfinishedItem != null)
                foreach (var props in UnfinishedItem)
                {
                    Console.WriteLine($"创建时间：{props.CreatTime} 内容：{props.Content}" +
                        $" 完成状态：{props.State} 过期时间：{props.TimeOfExpiration}" +
                        $" 备注：{props.Mark}");
                }
            else
                Console.WriteLine("输入错误或都已完成");
        }
        /// <summary>
        /// 打印所有事项
        /// </summary>
        /// <param name="ID"></param>
        public static void PrintAllItem(int ID)
        {
            UserLogic user = new UserLogic();
            var itemList = user.ViewAllItemList(ID);
            if (itemList != null)
            {
                foreach (var props in itemList)
                {
                    Console.WriteLine($"创建时间：{props.CreatTime} 内容：{props.Content}" +
                        $" 完成状态：{props.State} 过期时间：{props.TimeOfExpiration}" +
                        $" 备注：{props.Mark}");
                }
            }
            else
                Console.WriteLine("输入错误");
        }
        /// <summary>
        /// 打印过期事项
        /// </summary>
        /// <param name="ID"></param>
        public static void PrintItemOfPast(int ID)
        {
            UserLogic user = new UserLogic();
            var outdataItem = user.ItemRemind(Initialize.ItemList, ID);
            if (outdataItem != null)
                foreach (var props in outdataItem)
                {
                    Console.WriteLine($"创建时间：{props.CreatTime} 内容：{props.Content}" +
                        $" 完成状态：{props.State} 过期时间：{props.TimeOfExpiration}" +
                        $" 备注：{props.Mark}");
                }
            else
                Console.WriteLine("所有事项还未过期");
        }

        /// <summary>
        /// 打印创建清单所需
        /// </summary>
        public static void PrintCreateInventory()
        {
            InventoryLogic inventory = new InventoryLogic();           
            Console.Write("输入用户ID：");
            int id = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入清单名称：");
            string listName = Console.ReadLine();
            if (inventory.CreateInventory(listName, id))
            {
                Console.WriteLine("创建成功！！！");

                var inventoryList = Initialize.UserList.SingleOrDefault(user => user.UserId == id);
                List<Inventory> list = new List<Inventory>();
                list = inventoryList.UserInventoryList;
                foreach (var props in list)
                   Console.WriteLine($"{props.InventoryId}.清单名字：{props.ListName} 创建用户：{props.CreatName}" +
                       $" 创建时间：{props.CreatTime} ");
            }
            else
                Console.WriteLine("创建失败");
        }
        /// <summary>
        /// 打印添加事项所需
        /// </summary>
        public static void PrintAddItem()
        {
            InventoryLogic inventory = new InventoryLogic();
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入添加内容：");
            string content = Console.ReadLine();
            Console.WriteLine("输入备注：");
            string mark = Console.ReadLine();
            Console.Write("输入过期时间(年/月/日 小时：分钟：秒)：");
            DateTime time = DateTime.Parse(Console.ReadLine());
            if (inventory.AddItem(inventoryId, content, mark, time))
            {
                Console.WriteLine("创建成功！！！");
                var inventoryList = Initialize.InventoryList.SingleOrDefault(user => user.InventoryId == inventoryId);
                List<Item> list = new List<Item>();
                list = inventoryList.ItemListOfInventory.ToList();
                foreach (var props in list)
                    Console.WriteLine($"{props.ItemId}. 创建用户：{props.UserID} 事项内容：{props.Content}" +
                        $" 创建时间：{props.CreatTime} 过期时间：{props.TimeOfExpiration} 状态：{props.State} 备注：{props.Mark}");
            }                
            else
                Console.WriteLine("创建失败");
        }
        /// <summary>
        /// 打印删除事项所需
        /// </summary>
        public static void PrintDeleteItem()
        {
            InventoryLogic inventory = new InventoryLogic();
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入事项ID:");
            int itemId = Convert.ToInt32(Console.ReadLine());
            if (inventory.DeleteItem(itemId, inventoryId))
                Console.WriteLine("删除成功！！！");
            else
                Console.WriteLine("删除失败");
        }
        /// <summary>
        ///打印修改清单名称所需
        /// </summary>
        public static void PrintModification()
        {
            InventoryLogic inventory = new InventoryLogic();
            Console.Write("输入用户ID:");
            int userId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入新的清单的名字：");
            string newInventoryName = Console.ReadLine();
            if (inventory.Modification(userId, inventoryId, newInventoryName))
                Console.WriteLine("修改成功！！！");
            else
                Console.WriteLine("修改失败");
        }

        /// <summary>
        /// 打印添加备注所需
        /// </summary>
        public static void PrintAddMark()
        {
            ItemLogic item = new ItemLogic();
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入事项ID：");
            int itemId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入备注：");
            string mark = Console.ReadLine();
            if (item.AddMark(inventoryId, itemId, mark))
                Console.WriteLine("添加成功！！！");
            else
                Console.WriteLine("添加失败");
        }
        /// <summary>
        /// 打印添加过期时间所需
        /// </summary>
        public static void PrintAddTimeOfExpiration()
        {
            ItemLogic item = new ItemLogic();
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入事项ID：");
            int itemId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入过期时间(年/月/日 小时：分钟：秒)：");
            DateTime timeOfExpiration = DateTime.Parse(Console.ReadLine());
            if (item.AddTimeOfExpiration(inventoryId, itemId, timeOfExpiration))
                Console.WriteLine("添加成功！！！");
            else
                Console.WriteLine("添加失败");
        }
        /// <summary>
        /// 打印完成事项所需
        /// </summary>
        public static void PrintFinish()
        {
            ItemLogic item = new ItemLogic();
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入事项ID：");
            int itemId = Convert.ToInt32(Console.ReadLine());
            if (item.Finish(inventoryId, itemId))
                Console.WriteLine("已完成！！！");
            else
                Console.WriteLine("完成失败");
        }
        /// <summary>
        /// 打印取消完成所需
        /// </summary>
        public static void PrintCancel()
        {
            ItemLogic item = new ItemLogic();
            Console.Write("输入清单ID:");
            int inventoryId = Convert.ToInt32(Console.ReadLine());
            Console.Write("输入事项ID：");
            int itemId = Convert.ToInt32(Console.ReadLine());
            if (item.Cancel(inventoryId, itemId))
                Console.WriteLine("已取消完成！！！");
            else
                Console.WriteLine("取消失败   请检查你的输入是否有误");
        }
        public static void UserSystem()
        {
            int ID = PrintLogic();
            if (ID != 0)
            {
                Console.WriteLine("登陆成功！！！");
                do
                {
                    Console.WriteLine("1.用户未完成事项统计\t2.查看所有事项\n3.用户已过期事项统计\t4.跳转至清单处理");
                    int number = Convert.ToInt32(Console.ReadLine());
                    if (number == 1)
                        PrintUnfinishedItem();
                    else if (number == 2)
                        PrintAllItem(ID);
                    else if (number == 3)
                        PrintItemOfPast(ID);
                    else
                    {
                        InventorySystem();
                    }
                } while (true);
            }
            else
                Console.WriteLine("账号或密码错误");

        }
        public static void InventorySystem()
        {
            do
            {
                Console.WriteLine("1.创建清单\t2.添加事项\n3.删除事项\t4.修改清单名称\t5.跳转");
                int number1 = Convert.ToInt32(Console.ReadLine());
                switch (number1)
                {
                    case 1: PrintCreateInventory(); break;
                    case 2: PrintAddItem(); break;
                    case 3: PrintDeleteItem(); break;
                    case 4: PrintModification(); break;
                    case 5:ItemSystem();break;
                    default: Environment.Exit(0); break;
                }
            } while (true);      
        }
        public static void ItemSystem()
        {
            do
            {
                Console.WriteLine("1.添加备注\t2.添加过期时间\n3.完成事项\t4.取消完成");
                int number2 = Convert.ToInt32(Console.ReadLine());
                switch (number2)
                {
                    case 1: PrintAddMark(); break;
                    case 2: PrintAddTimeOfExpiration(); break;
                    case 3: PrintFinish(); break;
                    case 4: PrintCancel(); break;
                    default: Environment.Exit(0); break;
                }

            } while (true);
        }
    }
}